//
//  MBStorySceneCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStorySceneCell.h"

@interface MBStorySceneCell (){
    UIScrollView        *_bgView;
    UIImageView         *_img1;
    UILabel             *_lb1;
    UIButton            *_bt1;
    UIImageView         *_img2;
    UILabel             *_lb2;
    UIButton            *_bt2;
    UIImageView         *_img3;
    UILabel             *_lb3;
    UIButton            *_bt3;
    UIImageView         *_img4;
    UILabel             *_lb4;
    UIButton            *_bt4;
}

@end
@implementation MBStorySceneCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.showsHorizontalScrollIndicator = NO;
    _bgView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:_bgView];
    
    for (int i=0 ;i<4; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        imgView.backgroundColor = [UIColor clearColor];
        imgView.layer.cornerRadius = 5.f;
        imgView.layer.masksToBounds = YES;
        [_bgView addSubview:imgView];
        
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectZero];
        lb.backgroundColor = [UIColor clearColor];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.textColor = [UIColor blackColor];
        lb.font = [UIFont systemFontOfSize:13.];
        [_bgView addSubview:lb];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.backgroundColor = [UIColor clearColor];
        [bt addTarget:self action:@selector(onSelectItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:bt];
        
        if (i == 0) {
            _img1 = imgView;
            _lb1 = lb;
            _bt1 = bt;
        }
        else if (i == 1) {
            _img2 = imgView;
            _lb2 = lb;
            _bt2 = bt;
        }
        else if (i == 2) {
            _img3 = imgView;
            _lb3 = lb;
            _bt3 = bt;
        }
        else if (i == 3) {
            _img4 = imgView;
            _lb4 = lb;
            _bt4 = bt;
        }
    }
    
}

- (void)setType:(MBStorySceneCellType)type
{
    _type = type;
    [self setNeedsLayout];
}

- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
   [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)onSelectItemAction:(UIButton *)bt
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlByType:withValue:withView:)]) {
        NSString *value = @"0";
        if (bt == _bt1) {
            value = @"1";
        }
        else if (bt == _bt2) {
            value = @"2";
        }
        else if (bt == _bt3) {
            value = @"3";
        }
        else if (bt == _bt4) {
            value = @"4";
        }
        [_delegate onSelectControlByType:_type withValue:value withView:self];
    }

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //value
    for (int i=0; i<_dataArr.count; i++) {
        NSDictionary *dic = [_dataArr objectAtIndex:i];
        if (i==0) {
            _img1.image = [UIImage imageNamed:[dic objectForKey:@"pic"]];
            _lb1.text = [dic objectForKey:@"text"];
        }
        else if (i==1) {
            _img2.image = [UIImage imageNamed:[dic objectForKey:@"pic"]];
            _lb2.text = [dic objectForKey:@"text"];
        }
        else if (i==2) {
            _img3.image = [UIImage imageNamed:[dic objectForKey:@"pic"]];
            _lb3.text = [dic objectForKey:@"text"];
        }
        else if (i==3) {
            _img4.image = [UIImage imageNamed:[dic objectForKey:@"pic"]];
            _lb4.text = [dic objectForKey:@"text"];
        }

    }
    
    //frame
    float itemWidth = (ISIPAD?200*PERCEN:160*PERCEN);
    float itemHeight = (ISIPAD?284*PERCEN:264*PERCEN);
    float itemleft = (ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):(ISPortrait?40*PERCEN:(SCREEN_WIDTH-4*itemWidth)/5.));
    float itemk = (ISIPAD?(SCREEN_WIDTH-itemWidth*3-itemleft*2)/2.:(ISPortrait?50*PERCEN:itemleft));
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, itemHeight);
    _bgView.contentSize = CGSizeMake(itemleft*2+4*itemWidth+3*itemk, _bgView.height);
    
    _img1.frame = CGRectMake(itemleft, 30*PERCEN, itemWidth, (ISIPAD?180*PERCEN:160*PERCEN));
    _lb1.frame = CGRectMake(_img1.left, (WIDTH_5S ? _img1.bottom +5: _bgView.height-24*PERCEN-20), _img1.width, 20);
    _bt1.frame = CGRectMake(_img1.left, 0, _img1.width, _bgView.height);
    
    _img2.frame = CGRectMake(_img1.right+itemk, _img1.top, _img1.width, _img1.height);
    _lb2.frame = CGRectMake(_img2.left, _lb1.top, _img2.width, 20);
    _bt2.frame = CGRectMake(_img2.left, 0, _img2.width, _bgView.height);
    
    _img3.frame = CGRectMake(_img2.right+itemk, _img1.top, _img1.width, _img1.height);
    _lb3.frame = CGRectMake(_img3.left, _lb1.top, _img3.width, 20);
    _bt3.frame = CGRectMake(_img3.left, 0, _img3.width, _bgView.height);
    
    _img4.frame = CGRectMake(_img3.right+itemk, _img1.top, _img1.width, _img1.height);
    _lb4.frame = CGRectMake(_img4.left, _lb1.top, _img4.width, 20);
    _bt4.frame = CGRectMake(_img4.left, 0, _img4.width, _bgView.height);
    if (ISIPAD) {
        itemWidth = 160*PERCEN;
        itemHeight = 264*PERCEN;
        itemleft = 440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        itemk = (SCREEN_WIDTH-itemWidth*4-itemleft*2)/3.;
        
        _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, itemHeight);
        _bgView.contentSize = CGSizeMake(_bgView.width, _bgView.height);
        
        _img1.frame = CGRectMake(itemleft, 30*PERCEN, itemWidth, itemWidth);
        _lb1.frame = CGRectMake(_img1.left,_bgView.height-30*PERCEN-20, _img1.width, 20);
        _bt1.frame = CGRectMake(_img1.left, 0, _img1.width, _bgView.height);
        
        _img2.frame = CGRectMake(_img1.right+itemk, _img1.top, _img1.width, _img1.height);
        _lb2.frame = CGRectMake(_img2.left, _lb1.top, _img2.width, 20);
        _bt2.frame = CGRectMake(_img2.left, 0, _img2.width, _bgView.height);
        
        _img3.frame = CGRectMake(_img2.right+itemk, _img1.top, _img1.width, _img1.height);
        _lb3.frame = CGRectMake(_img3.left, _lb1.top, _img3.width, 20);
        _bt3.frame = CGRectMake(_img3.left, 0, _img3.width, _bgView.height);
        
        _img4.frame = CGRectMake(_img3.right+itemk, _img1.top, _img1.width, _img1.height);
        _lb4.frame = CGRectMake(_img4.left, _lb1.top, _img4.width, 20);
        _bt4.frame = CGRectMake(_img4.left, 0, _img4.width, _bgView.height);

    }
}

@end
